home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 361_01 / gadgets.txt < prev    next >
Text File  |  1991-09-18  |  50KB  |  1,558 lines

  1.  
  2.  
  3. /* Gadgetx.lib ---> A Custom Turbo-C Library "Bag of Tricks".
  4.  *
  5.  * J.Ekwall                        30 August 91
  6.  *
  7.  * Copyrighted to the Public Domain.  Unlimited    Distribution Authorized.
  8.  *
  9.  * User    Assumes    All Risks/Liabilities.
  10.  *
  11.  * Last    Update:    16 September 91/EK
  12.  */
  13.  
  14. ============================================================================
  15.  
  16.    The need for    "Gadgets" to extend the    reach of ANSI-C    has spun off many 
  17. commercially available "External" Libraries, among these C-Express and 
  18. Vitamin-C.  The    need to    freely distribute Tools    limits their usefulness, 
  19. particularly in    our current User-Hostile ADPE environment.
  20.  
  21.    These routines are Public Domain, either home-grown,    taken from other 
  22. Public Domain sources (such as C User's    Croup (CUG)), taken from Publications
  23. or lifted from Shareware and significantly modified/enhanced (nullifying 
  24. the original copyright).  CUG sources are identified by    their distribution 
  25. disk number, ie. [CUG-273] is "TurboC Utilities" developed By Jim Derr,
  26. 2425 Santa Cruz    Ct., Santa Rosa, Ca. 95401.
  27.  
  28. ========================================================================
  29.  
  30. Notes:
  31.     1. On the CRT, X ranges 1-80, Y ranges 1-25.
  32.     2. Direct Video Routines use Vpeek/Vpoke which are hardwired for
  33.        Color CGA/EGA/VGA Video RAM based at 0xB800.
  34.  
  35. -------------------------------------------------------------------------
  36.  
  37. >AnyCharacter.  Pop a Query Window for an Ascii    Value.
  38.  
  39. int AnyCharacter(void)
  40. { /* Create Any    Character */
  41.     int    c, ch =    0, n = 3, xx, yy;
  42.     char FootPrint[216];
  43.  
  44.  /* Ask    'em */
  45.     Getxy(&xx, &yy);
  46.     MkAskBox(FootPrint,    28, 11,    26, 3, 'L', "[ ASCII Value ]",
  47.        "[ <ESC>    to Cancel ]", "Enter Three Digits: ", 0x4E);
  48.  
  49.  /* Catch Response, Build Character & Split */
  50.     while (n &&    (c = Kbq_read()) != ESC    && c !=    CR)
  51.        if (isdigit(c)) { ch = 10 * ch +    c - '0'; n--; DputChr(c, 0x4E);    }
  52.     ZapAskBox(FootPrint, 28, 11, 26, 3,    'L'); Gotoxy(xx, yy); ShowCursor(1);
  53.     if (c IS ESC) return 0; else return    ch & 0xFF;
  54. }
  55.  
  56. -------------------------------------------------------------------------
  57.  
  58. >BaseName.  Return a Pointer to    the Name.Ext portion of    a Full Path.
  59.  
  60. char *BaseName(char *FileName)
  61. {
  62.     char *tp1;
  63.     
  64.     if ((tp1 = strrchr(FileName, BKSL))    != NULL) return    ++tp1;
  65.     if ((tp1 = strrchr(FileName, COLON)) != NULL) return ++tp1;
  66.     return FileName;
  67. }
  68.  
  69. -------------------------------------------------------------------------
  70.  
  71. >Beep.  Sound Tone for Specified Duration (in milliSeconds).
  72.  
  73. void Beep(unsigned int pitch, unsigned int Duration)
  74. { sound(pitch);    delay(Duration); nosound(); }
  75.   
  76. -------------------------------------------------------------------------
  77.  
  78. >Box.  Draw/Clr    a Single Line Box on the Screen.          [CGU-273]
  79.  
  80. void Box(int Left, int Top, int    Right, int Bottom, int Fill, int Color)
  81. /* Draw    a single line box using    Color &    Clear it to Fill. */
  82. {
  83.     int    i;
  84.     char Lid[81];
  85.  
  86.     Dwrite(Left, Top, Color, "┌"); Dwrite(Right, Top, Color, "┐");
  87.     Dwrite(Left, Bottom, Color,    "└"); Dwrite(Right, Bottom, Color, "┘");
  88.     *Lid = NULL; PadRight(Lid, '─', Right - Left - 1);
  89.     Dwrite(Left+1, Top,    Color, Lid); Dwrite(Left+1, Bottom, Color, Lid);
  90.     for    (i= Top    + 1; i < Bottom; i++) {
  91.     Dwrite(Left, i, Color, "│"); Dwrite(Right,    i, Color, "│");    }
  92.     ClrBox(++Left, ++Top, --Right, --Bottom, Fill);
  93. }
  94.  
  95. -------------------------------------------------------------------------
  96.  
  97. >CcolorSet.  Change the    Color of a Column of Characters.     [CGU-273]
  98.  
  99. void CcolorSet(int X, int Y, int Color,    int N)
  100. { while    (N--) if (Y < 26) SetAttr(X, Y++, Color); }
  101.  
  102. -------------------------------------------------------------------------
  103.  
  104. >CgaSnowFence.  Activate ReTrace Waiting on Video RAM Access.
  105.  
  106. void CgaSnowFence(void)    { SnowStop++; }
  107.  
  108. -------------------------------------------------------------------------
  109.  
  110. >Clr.  Clear Entire Screen to a    Specified Color.
  111.  
  112. #define    Clr(x)            (ClrBox(1, 1, 80, 25, x))
  113.  
  114. -------------------------------------------------------------------------
  115.  
  116. >ClrTo.  Clears    a Row to a Specified Point.
  117.  
  118. void ClrTo(int X, int Color)
  119. /* Clear from the current cursor location thru X. */
  120. {
  121.     int    xx, yy;
  122.     
  123.     Getxy(&xx, &yy); if    (X < xx) return; Scroll(xx, yy,    X, yy, Color, 0, 0);
  124. }
  125.  
  126. -------------------------------------------------------------------------
  127.  
  128. >ClrBox.  Clear    a Retangular Area to a Specified Color.
  129.  
  130. #define    ClrBox(a,b,c,d,x)    (Scroll(a, b,    c, d, x, 0, 0))
  131.  
  132. -------------------------------------------------------------------------
  133.  
  134. >CursorBwd.  Left Arrow    Action w/ Wrap Upwards.              [CGU-273]
  135.  
  136. void CursorBwd(int N)
  137. /* Move    the cursor N cols to the left */
  138. {
  139.     int    xx, yy;
  140.  
  141.     Getxy(&xx, &yy);
  142.     if ((xx -= (N % 80)) < 1) {    xx -= 80; if (--yy < 1)    yy = 25; }
  143.     Gotoxy(xx, yy);
  144. }
  145.  
  146. -------------------------------------------------------------------------
  147.  
  148. >CursorDn.  Down Arrow Action w/ Wrap.                  [CGU-273]
  149.  
  150. void CursorDn(int N)
  151. /* Move    the down N rows    & wrap to top if the cursor is on the last row */
  152. {
  153.     int    xx, yy;
  154.     
  155.     Getxy(&xx, &yy); if    ((yy +=    (N % 25)) > 25)    yy -= 25; Gotoxy(xx, yy);
  156. }
  157.  
  158. -------------------------------------------------------------------------
  159.  
  160. >CursorFwd.  Right Arrow Action    w/ Wrap    Downward.          [CGU-273]
  161.  
  162. void CursorFwd(int N)
  163. /* Move    the cursor N cols to the right */
  164. {
  165.     int    xx, yy;
  166.  
  167.     Getxy(&xx, &yy);
  168.     if ((xx += (N % 80)) > 80) { xx = 1; if (++yy > 25)    yy = 1;    }
  169.     Gotoxy(xx, yy);
  170. }
  171.  
  172. -------------------------------------------------------------------------
  173.  
  174. >CursorNL.  Perform a CR/LF Action w/ Wrap.              [CGU-273]
  175.  
  176. void CursorNL(int N) 
  177. /* Move    the cursor to the beginning of the next    row  */
  178. {
  179.     int    xx, yy;
  180.     
  181.     Getxy(&xx, &yy); if    ((yy +=    (N % 25)) > 25)    yy = 1;    Gotoxy(0, yy);
  182. }
  183.  
  184. -------------------------------------------------------------------------
  185.  
  186. >CursorUP.  Up Arrow Aaction w/    Wrap.                  [CGU-273]
  187.  
  188. void CursorUP(int N)
  189. /* Move    the cursor N rows up */
  190. {
  191.     int    xx, yy;
  192.  
  193.     Getxy(&xx, &yy); if    ((yy -=    (N % 25)) < 1) yy = 25;    Gotoxy(xx, yy);
  194. }
  195.  
  196. -------------------------------------------------------------------------
  197.  
  198. >DputChr.  Direct Write    a Character at Cursor w/ Update.
  199.  
  200. void DputChr(char ch, int Color)
  201. {
  202.     int    xx, yy;
  203.  
  204.     Getxy(&xx, &yy); Vpoke(Vaddr(xx,yy), ch + (Color <<    8)); CursorFwd(1);
  205. }
  206.  
  207. -------------------------------------------------------------------------
  208.  
  209. >Dread.  Copy N    Characters to a    String Starting    at (X,Y).
  210.  
  211. void Dread(int X, int Y, char *Text, int N)
  212. {
  213.     int    Addr;
  214.  
  215.     Addr = Vaddr(X, Y);
  216.     for    (*Text = NULL; N-- && X++ < 80;    *Text =    NULL, Addr += 2)
  217.     *Text++    = Vpeek(Addr) &    0xFF;
  218. }
  219.  
  220. -------------------------------------------------------------------------
  221.  
  222. >Dwrite.  Paint    a String at X,Y    w/ Color w/ Update.
  223.   
  224. void Dwrite(int    X, int Y, int Color, char *Text)
  225. {
  226.     int     addr;
  227.  
  228.     Color = Color << 8;    Gotoxy(X, Y); CursorFwd(strlen(Text));
  229.     for    (addr =    Vaddr(X, Y); *Text; addr += 2)
  230.        Vpoke(addr, (*Text++ & 0xFF) + Color);
  231. }
  232.  
  233. -------------------------------------------------------------------------
  234.  
  235. >DwriteEnd.  Dwrite w/ Pad to Fill "n" Spaces.
  236.  
  237. void DwriteEnd(int X, int Y, int Color,    char *Text, int    N)
  238. {
  239.     int     addr;
  240.  
  241.     Color = Color << 8;    Gotoxy(X, Y); CursorFwd(strlen(Text));
  242.     for    (addr =    Vaddr(X, Y); *Text; addr += 2, --N)
  243.        Vpoke(addr, (*Text++ | Color));
  244.     for    (Color |= ' '; N--; addr += 2) Vpoke(addr, Color);
  245. }
  246.  
  247. -------------------------------------------------------------------------
  248.  
  249. >ExpandTabs.  Explode Tabs into    Spaces ala X3.64/DEC/DOS.
  250.  
  251. void ExpandTabs(char *Text)
  252. {
  253.     int    i;
  254.     
  255.     for    (i = 0;    Text[i]; i++)
  256.        if (Text[i] IS HT) { 
  257.       Text[i++] = SPACE; OpenStr(Text + i, (8 - (i % 8)) % 8); }
  258. }
  259.  
  260. -------------------------------------------------------------------------
  261.  
  262. >FileExists.  TRUE if File is Found.                   [CGU-273]
  263.  
  264. int FileExists(char *FileName)
  265. /* Check to see    of a Find exists. RETURN 1 if exist 0 if not. */
  266. {
  267.     struct ffblk fb;
  268.     
  269.     return !findfirst(FileName,    &fb, 0);
  270. }
  271.